﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Threading;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for PlayerView.xaml
    /// </summary>
    public partial class PlayerView : Page
    {
        #region "Properties"
        TimeSpan pos;
        DispatcherTimer timer = new DispatcherTimer();
        DispatcherTimer contentTimer = new DispatcherTimer();
        MainWindow mainWindow = null;

        public bool menuMasked
        {
            get;
            set;
        }

        public bool isActive
        {
            get;
            set;
        }

        public bool contentLoaded
        {
            get;
            set;
        }

        public bool lecture
        {
            get;
            set;
        }

        public bool pause
        {
            get;
            set;
        }

        public bool mute
        {
            get;
            set;
        }

        public string chemin
        {
            get;
            set;
        }

        public bool clickDown
        {
            get;
            set;
        }
        #endregion

        #region "Player Actions"
        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="window"></param>
        public PlayerView(MainWindow window)
        {
            InitializeComponent();

            clickDown = false;
            chemin = null;
            pause = false;
            lecture = false;
            contentLoaded = false;
            isActive = false;
            menuMasked = false;
            mute = false;

            mainWindow = window;
            mainWindow.lbl_time.Visibility = System.Windows.Visibility.Hidden;

            contentTimer.Tick += new EventHandler(detection);
        }

        /// <summary>
        /// Fonction appeler pour couper le volume
        /// </summary>
        public void stopVolume()
        {
            if (contentLoaded == true)
            {
                lecteur.IsMuted = true;
                lecteur.Volume = 0;
            }
        }

        /// <summary>
        /// Fonction permettant le reset du volume
        /// </summary>
        public void resetVolume()
        {
            if (contentLoaded == true)
            {
                lecteur.IsMuted = false;
                lecteur.Volume = 0.5;
            }
        }

        /// <summary>
        /// Fonction appeler pr la gestion de l'evenement souris lors de la lecture du media
        /// </summary>
        private void detectActivity(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isActive == true && contentLoaded == true)
            {
                contentTimer.Stop();

                mainWindow.Cursor = System.Windows.Input.Cursors.Arrow;
                mainWindow.timeline.Visibility = System.Windows.Visibility.Visible;
                mainWindow.stackpanel.Visibility = System.Windows.Visibility.Visible;

                mainWindow.timeline.Height = 25;
                mainWindow.stackpanel.Height = 40;

                menuMasked = false;
                contentTimer.Interval = TimeSpan.FromSeconds(3);
                contentTimer.Start();
            }
        }

        /// <summary>
        /// Fonction appeler pour cacher le bandeau de lecture
        /// </summary>
        private void detection(object sender, EventArgs e)
        {
            if (isActive == false || contentLoaded == false)
            {
                contentTimer.Stop();
                return;
            }

            mainWindow.Cursor = System.Windows.Input.Cursors.None;
            mainWindow.timeline.Visibility = System.Windows.Visibility.Hidden;
            mainWindow.stackpanel.Visibility = System.Windows.Visibility.Hidden;

            mainWindow.timeline.Height = 0;
            mainWindow.stackpanel.Height = 0;

            menuMasked = true;
        }

        /// <summary>
        /// Fonction pour activer la Pause/Play
        /// </summary>
        public void interrupt()
        {
            try
            {
                if (lecture == true && pause == false && contentLoaded == true)
                {
                    lecteur.LoadedBehavior = MediaState.Pause;
                    pause = true;

                    mainWindow.interruptImage.Source = new BitmapImage(new Uri("../../Images/play_button.png", UriKind.Relative));
                }
                else if ((lecture == true && pause == true) || (lecture == false && pause == false))
                {
                    if (chemin != null)
                    {
                        lecteur.LoadedBehavior = MediaState.Play;

                        lecteur.Opacity = 1.0;
                        if (lecteur.HasAudio == true)
                            mainWindow.lbl_time.Visibility = System.Windows.Visibility.Visible;
                        lecture = true;
                        pause = false;
                    }

                    mainWindow.interruptImage.Source = new BitmapImage(new Uri("../../Images/pause_button.png", UriKind.Relative));
                }
                else if (lecture == false && contentLoaded == true)
                {
                    startMedia();
                    lecteur.LoadedBehavior = MediaState.Play;
                    lecture = true;
                }

            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// Fonction permettant l'arret du media
        /// </summary>
        public void stop()
        {
            try
            {
                if (lecture == true)
                {
                    if (mainWindow.WindowState == WindowState.Maximized)
                    {
                        mainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                        mainWindow.WindowState = WindowState.Normal;
                        mainWindow.timeline.Visibility = System.Windows.Visibility.Visible;
                        mainWindow.stackpanel.Visibility = System.Windows.Visibility.Visible;
                    }

                    lecteur.LoadedBehavior = MediaState.Stop;
                    lecture = false;
                    pause = false;
                    mainWindow.timeline.Value = 0;
                    lecteur.Opacity = 0.0;
                    mainWindow.interruptImage.Source = new BitmapImage(new Uri("../../Images/play_button.png", UriKind.Relative));

                    mainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                    mainWindow.WindowState = WindowState.Normal;

                    mainWindow.lbl_time.Visibility = System.Windows.Visibility.Hidden;
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// Fonction permettant de mettre en plein ecran
        /// </summary>
        public void fullScreen()
        {
            try
            {
                if (mainWindow.WindowState == WindowState.Maximized)
                {
                    mainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                    mainWindow.WindowState = WindowState.Normal;
                    mainWindow.playView.Margin = new Thickness(0, 0, 0, 50);

                    if (menuMasked == false)
                    {
                        mainWindow.timeline.Visibility = System.Windows.Visibility.Visible;
                        mainWindow.stackpanel.Visibility = System.Windows.Visibility.Visible;

                        mainWindow.timeline.Height = 25;
                        mainWindow.stackpanel.Height = 40;
                    }
                }
                else
                {
                    mainWindow.WindowStyle = WindowStyle.None;
                    mainWindow.WindowState = WindowState.Maximized;
                    mainWindow.playView.Margin = new Thickness(0, 0, 0, 0);

                    if (menuMasked == true)
                    {
                        mainWindow.timeline.Visibility = System.Windows.Visibility.Hidden;
                        mainWindow.stackpanel.Visibility = System.Windows.Visibility.Hidden;

                        mainWindow.timeline.Height = 0;
                        mainWindow.stackpanel.Height = 0;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// Fonction appeler quand est media ne peut pas etre chargee
        /// </summary>
        private void mediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.ErrorException.Message);
        }

        /// <summary>
        /// Fonction permettant de lancer le media
        /// </summary>
        private void startMedia()
        {
            try
            {
                lecteur.Opacity = 1.0;

                if (lecteur.HasAudio == true)
                    mainWindow.lbl_time.Visibility = System.Windows.Visibility.Hidden;

                timer.Interval = TimeSpan.FromMilliseconds(1000);
                timer.Tick += new EventHandler(mainWindow.ticktock);

                mainWindow.timeline.Minimum = 0;
                pos = lecteur.NaturalDuration.TimeSpan;
                mainWindow.timeline.Maximum = pos.TotalSeconds;
                mainWindow.lbl_time.Content = lecteur.Position.ToString();

                timer.Start();
                contentTimer.Interval = TimeSpan.FromSeconds(3);

                if (lecteur.HasAudio == true)
                {
                    mainWindow.lbl_time.Visibility = System.Windows.Visibility.Visible;
                    mainWindow.timeline.IsEnabled = true;
                    contentTimer.Start();
                }
                contentLoaded = true;

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Message : " + ex.Message);
            }
        }

        /// <summary>
        /// Fonction appeler lorsque le media a reussi a s'ouvrir
        /// </summary>
        private void mediaOpened(object sender, RoutedEventArgs e)
        {
            startMedia();

            interrupt();
        }

        /// <summary>
        /// Fonction qui reinitialise les proprietees du player
        /// </summary>
        public void endMedia()
        {
            lecteur.LoadedBehavior = MediaState.Stop;
            lecteur.Opacity = 0.0;
            lecture = false;

            mainWindow.interruptImage.Source = new BitmapImage(new Uri("../../Images/play_button.png", UriKind.Relative));

            timer.Stop();
            mainWindow.timeline.Value = 0;
            mainWindow.btn_LibraryView.IsEnabled = true;
            mainWindow.lbl_time.Visibility = System.Windows.Visibility.Hidden;
            mainWindow.timeline.IsEnabled = false;
            contentLoaded = false;
        }

        /// <summary>
        /// Fonction appeler a la fin du media
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mediaEnded(object sender, EventArgs e)
        {
            endMedia();
            mainWindow.nextMedia();
        }
        #endregion
    }
}